﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web
{
    public partial class TopicInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id; if (int.TryParse(Request.QueryString["id"], out id))
                {
                    this.hidTopicId.Value = id.ToString();
                    bindData(id);
                }
            }
        }

        private void bindData(int id)
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            System.Data.DataTable dt = dal.GetTopic(id);
            if (dt.Rows.Count > 0)
            {
                this.litTitle.Text = dt.Rows[0]["Title"].ToString();
                this.litContent.Text = dt.Rows[0]["Content"].ToString();
                switch (dt.Rows[0]["type"].ToString())
                { 
                    case "活动召集":
                        HdSign.Visible = true;
                        voteInfo.Visible = false;
                        bindHdInfo(id);
                        break;
                    case "社区议题":
                    case "业主大会":
                    case "维修基金":
                        voteInfo.Visible = true;
                        HdSign.Visible = false;
                        bindGrid(id, dt.Rows[0]["votetype"].ToString());
                        break;
                    default:
                        voteInfo.Visible = false;
                        HdSign.Visible = false;
                        break;
                }
            }
        }

        private void bindGrid(int id,string votetype)
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.dgdVoteItem.DataSource = dal.GetTopicVoteInfo(id);
            this.dgdVoteItem.DataBind();
            if (dal.GetTopicIsSelfVote(id))
            {
                this.cmdSubmit.Text = " 您已经投过票了！";
                dgdVoteItem.Columns[0].Visible = false;
                dgdVoteItem.Columns[1].Visible = true;
                dgdVoteItem.Columns[2].Visible = true;
                dgdVoteItem.Columns[3].Visible = true;
                this.cmdSubmit.Enabled = false;
                this.lblRem.Visible = false;
                this.txtRem.Visible = false;
                if (votetype == "投完不可查看结果") {
                    this.dgdVoteItem.Visible = false;
                }
            }
            else
            {
                dgdVoteItem.Columns[0].Visible = true;
                dgdVoteItem.Columns[1].Visible = false;
                dgdVoteItem.Columns[2].Visible = false;
                dgdVoteItem.Columns[3].Visible = false;
            }
        }

        private void bindHdInfo(int id)
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            bool isSelfSign = false; int iSignTotal = 0;
            iSignTotal = dal.GetTopicHDnum(id, out isSelfSign);
            if (isSelfSign) { this.cmdSign.Text = "  您已经报名了！"; this.cmdSign.Enabled = false;
            this.lblRem1.Visible = false; this.txtRem1.Visible = false;
            }
            this.lblSignInfo.Text = "已有 " + iSignTotal.ToString() + " 人报名！";
        }

        protected void cmdSubmit_Click(object sender, EventArgs e)
        {
            int voteItemId;
            if (this.hidVoteItemId.Value == string.Empty)
            {
                Common.JsAlert(this, "请选择投票项！");
                return ;
            }
            if (int.TryParse(this.hidVoteItemId.Value, out voteItemId))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                string rem = this.txtRem.Text.Trim();
                if (dal.AddTopicVote(int.Parse(this.hidTopicId.Value), voteItemId, rem))
                {
                    Common.JsAlert(this, "感谢您的参与！");
                    bindData(int.Parse(this.hidTopicId.Value));
                }
            }
            else
            {
                Common.JsAlert(this, "参数错误！");
                return;
            }
        }

        protected void cmdSign_Click(object sender, EventArgs e)
        {
            int topicId;
            if (int.TryParse(this.hidTopicId.Value, out topicId))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                string rem = this.txtRem1.Text.Trim();
                if (dal.AddTopicVote(topicId, 0, rem))
                {
                    Common.JsAlert(this, "感谢您的参与！");
                    bindData(topicId);
                }
            }
            else
            {
                Common.JsAlert(this, "参数错误！");
                return;
            }
        }
    }
}